import logging
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

from entity import entities
from dto import dto
from dto import factory


class MainPage(webapp.RequestHandler):
	
	def get(self):
		ingredients_query = entities.Ingredient.all()
		ingredients = ingredients_query.fetch(30)
		ingredients_dto = []
		for ingredient in ingredients:
			ingredients_dto.append( factory.DtoFactory.getIngredientDto(ingredient, "it") )
		
		view = "views/index.html"
		model = { "ingredients" : ingredients_dto }
		self.response.out.write(template.render(view, model))



def main():
	application = webapp.WSGIApplication( [('/', MainPage)], debug=True)
	wsgiref.handlers.CGIHandler().run(application)
	


#i1_n = entities.I18nString(language="it", text=u"Latte")
#i1_n.put()
#i12_n = entities.I18nString(language="en", text=u"Milk")
#i12_n.put()
#i1 = entities.Ingredient()
#i1.addName( i1_n.key() )
#i1.addName( i12_n.key() )
#i1.put()
#
#i2_n = entities.I18nString(language="it", text=u"Zucchero")
#i2_n.put()
#i22_n = entities.I18nString(language="en", text=u"Sugar")
#i22_n.put()
#i2 = entities.Ingredient()
#i2.addName( i2_n.key() )
#i2.addName( i22_n.key() )
#i2.put()

i1_n = entities.I18nString(language="it", text=u"Sale")
i1_n.put()
i12_n = entities.I18nString(language="en", text=u"Salt")
i12_n.put()
i1 = entities.Ingredient()
i1.addName( i1_n.key() )
i1.addName( i12_n.key() )
i1.put()

if __name__ == "__main__":
	main()
